/**
 * Index RPC
 */
import { ResDto } from "lh-hooks";
import { BaseConfig } from "../../interface/config/baseConfig";
import { UrlEnum } from "../../constants/enums/urlEnum";
import { IndexDto, IndexVo } from "../../interface/index/indexDto";
import { UniversalConfig } from "../../constants/config/universalConfig";

export class IndexRpc {
    private readonly request: Function;
    private config: BaseConfig;
    /**
     * 构造方法
     */
    constructor(config: BaseConfig, request: Function){
        this.request = request;
        this.config = config;
    }
    /**
     * 转换地址
     */
    private convertUrl(path: string){
        return this.config.gatewayUrl + path;
    }
    /**
     * 获取数据demo
     * @param params 入参对象
     */
    queryIndexData(params: any): Promise<IndexVo[]>{
        return new Promise((resolve, reject) => {
            this.request({
                url: this.convertUrl(UrlEnum.GET_DEMO_LIST),
                data: params
            }).then((res: ResDto<IndexDto[]>) => {
                res && res.code === UniversalConfig.SUCCESS_CODE ? resolve(res.data) : reject(res);
            }).catch((err: any) => reject(err));
        });
    }
}